﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;

namespace PPI.Controls.Grid
{
    public partial class GridDistributionTransformers : GridBase
    {
        private List<ETransformer> m_Transformers;

        public List<ETransformer> CurrentTransformers
        {
            get
            {
                return m_Transformers;
            }
        }

        public EDistributionPole Pole { get; set; }

        public GridDistributionTransformers()
        {
            InitializeComponent();
            cmiEditWhithOutImages.Visible = false;
        }

        public void LoadTransformers(string connectionString)
        {
            m_Transformers = LTransformer.GetPoleTransformers(Pole, connectionString);
            LoadTransformers(m_Transformers);
        }

        public void LoadTransformers(List<ETransformer> transformers)
        {
            m_Transformers = transformers;
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            var dt = new DataTable("Transformer");

            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Serial Number", typeof(string)));
            dt.Columns.Add(new DataColumn("Utility Number", typeof(string)));

            foreach (ETransformer transformer in m_Transformers)
                dt.Rows.Add(transformer.Id, transformer.Latitude == double.MinValue ? 0 : transformer.Latitude, transformer.Longitude == double.MinValue ? 0 : transformer.Longitude, transformer.SerialNumber, transformer.UtilityNumber);

            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = dt;
            datagridBase.Refresh();
        }
    }
}
